﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;

namespace WFL.Utils
{
    public static class ConfigurationHelper
    {
        public static TSection GetSection<TSection>(string file, string name) where TSection : ConfigurationSection
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "配置节名称为空");
            }
            if (!string.IsNullOrEmpty(file))
            {
                var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file) };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                if (configuration == null)
                {
                    throw new Exception(string.Format("找不到配置文件\"{0}\"", file));
                }
                return (TSection)configuration.GetSection(name);
            }
            else
            {
                return (TSection)ConfigurationManager.GetSection(name);
            }
        }

        public static TSection GetSection<TSection>(string name) where TSection : ConfigurationSection
        {
            return GetSection<TSection>(null, name);
        }

        public static string GetAppSettingsValue(string file, string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "键值为空");
            }
            if (!string.IsNullOrEmpty(file))
            {
                var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file) };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                if (configuration == null)
                {
                    throw new Exception(string.Format("找不到配置文件\"{0}\"", file));
                }
                KeyValueConfigurationElement configElement = configuration.AppSettings.Settings[key];
                return configElement != null ? configElement.Value : null;
            }
            else
            {
                return ConfigurationManager.AppSettings[key];
            }
        }

        public static string GetAppSettingsValue(string key)
        {
            return GetAppSettingsValue(null, key);
        }

        public static ConnectionStringSettings GetConnectionStringSettings(string file, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "配置名称为空");
            }
            if (!string.IsNullOrEmpty(file))
            {
                var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file) };
                var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                if (configuration == null)
                {
                    throw new Exception(string.Format("找不到配置文件\"{0}\"", file));
                }
                return configuration.ConnectionStrings.ConnectionStrings[name];
            }
            else
            {
                return ConfigurationManager.ConnectionStrings[name];
            }
        }

        public static ConnectionStringSettings GetConnectionStringSettings(string name)
        {
            return GetConnectionStringSettings(null, name);
        }
    }
}
